//
//  CustomTabBar.m
//  CivilianTravel
//
//  Created by xalo on 16/7/12.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "CustomTabBar.h"
#import "ShareViewController.h"
#import "PassValue.h"
#import "LoginViewController.h"
@interface CustomTabBar ()

/** 发布按钮 */
@property (nonatomic, weak) UIButton *publishButton;

@end

@implementation CustomTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"shareSelect"] forState:UIControlStateHighlighted];
        [publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:publishButton];
        self.publishButton = publishButton;
    }
    return self;
}
-(void)publishClick{
    if (![[PassValue sharePassValue] username]) {
        
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil] animated:YES completion:nil];
    } else {
        ShareViewController *hare = [[ShareViewController alloc] init];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:hare animated:YES completion:nil];
    }
}


- (void)layoutSubviews{
    [super layoutSubviews];
    //设置发布按钮的fram
    self.publishButton.bounds = CGRectMake(0, 0, self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height);
    self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    //设置其他UITabBarBut
    CGFloat buttonY = 0;
    CGFloat buttonW = self.frame.size.width / 5;
    CGFloat buttonH = self.frame.size.height;
    NSInteger index = 0;
    for (UIView *button in self.subviews) {
        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
            //计算按钮的x值
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        //增加索引
        index++;
        
    }
}



@end
